/**
 * Created:	2011-8-21
 * By:		eeorange
 * OS:		Windows/mingw
 * Ques ID:	uva 567 - Risk
 * Status:	
 * Desc:	
 *
 */
#ifdef ONLINE_JUDGE
	#define NDEBUG
#endif
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
// STL
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

const int MAXN = 21;
const int INF = 1000000;
int st, en;
int d[MAXN];
bool G[MAXN][MAXN], done[MAXN];
typedef pair<int, int> pii;
priority_queue<pii, vector<pii>, greater<pii> > q;

bool read_input()
{
	memset(G, 0, sizeof(G));
	for(int x = 1; x <= 19; x++)
	{
		int n;
		if(scanf("%d", &n) != 1) return false;
		for(int i = 0; i < n; i++)
		{
			int y;
			cin >> y;
			G[x][y] = G[y][x] = true;
		}
	}

	return true;
}

void dijkstra()
{
	memset(done, 0, sizeof(done));
	for(int i = 1; i <= 20; i++) d[i] = (i == st ? 0 : INF);
	q.push(make_pair(d[st], st));
	while(!q.empty())
	{
		pii u = q.top(); q.pop();
		int x = u.second;
		if(done[x]) continue;
		done[x] = true;
		for(int y = 1; y <= 20; y++) if(G[x][y] && d[y] > d[x] + 1)
		{
			d[y] = d[x] + 1;
			q.push(make_pair(d[y], y));
		}
	}
}

int main(){
	#ifndef ONLINE_JUDGE
		freopen("data.in", "r", stdin);
	#endif

	int cnt = 0;
	while(read_input())
	{
		printf("Test Set #%d\n", ++cnt);

		int n;
		cin >> n;
		for(int idx = 1; idx <= n; idx++)
		{
			cin >> st >> en;
			dijkstra();
			printf("%2d to %2d: %d\n", st, en, d[en]);
		}
		printf("\n");
	}



	#ifndef ONLINE_JUDGE
		printf("Time used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC);
	#endif
	return 0;
}

